\chapter{Ap\'endice}
\subsection{joy.h}
\begin{listing}[language=C]
#include <stdio.h>
#ifndef __JOYSTICK_H__
#define __JOYSTICK_H__

#define JOYSTICK_DEVNAME "/dev/input/js0"

#define JS_EVENT_BUTTON         0x01    /* boton apretado/soltado */
#define JS_EVENT_AXIS           0x02    /* joystick movido */
#define JS_EVENT_INIT           0x80    /* estado inicial */
	
struct js_event {
        unsigned int time;      /* timestamp in milisegundos */
        short value;   		/* valor */
        unsigned char type;     /* Tipo de evento */
        unsigned char number;   /* axis/numero de boton */
};
	
struct wwvi_js_event {
        int button[11];
        int stick_x;
        int stick_y;
};
	
extern int open_joystick(/*char *joystick_device*/);
extern int read_joystick_event(struct js_event *jse);
extern void set_joystick_y_axis(int axis);
extern void set_joystick_x_axis(int axis);
extern void close_joystick();
extern int get_joystick_status(struct wwvi_js_event *wjse);
	
#endif
\end{listing}
\subsection{joy.c}
\bigskip
\begin{listing}[language=C]
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include "joy.h"
	
static int joystick_fd = -1;

/* Abro el dispositivo */	
int open_joystick()
{
	joystick_fd = open(JOYSTICK_DEVNAME, O_RDONLY | O_NONBLOCK);
	if (joystick_fd < 0)
	{
		return joystick_fd;
	}	
    return joystick_fd;
}
	
/* Lectura del dispositivo 
	@return 0 o -1 si hay un error
	@return 1 exito	
*/
int read_joystick_event(struct js_event *jse)
{
	int bytes;

	bytes = read(joystick_fd, jse, sizeof(*jse)); 
	if (bytes == -1)
	{
		return 0;
	}
	
	if (bytes == sizeof(*jse))
	{
		return 1;
	}
	
	printf("Unexpected bytes from joystick:%d\n", bytes);
	
    return -1;
}

/* Cierro el dispositivo */
	
void close_joystick()
{
	close(joystick_fd);
}
	
int get_joystick_status(struct wwvi_js_event *wjse)
{
	int rc;
    struct js_event jse;
    if (joystick_fd < 0)
    		return -1;
	
	while ((rc = read_joystick_event(&jse) == 1))
	{
		jse.type &= ~JS_EVENT_INIT; /* ignore synthetic events */
        if (jse.type == JS_EVENT_AXIS)
        {
			switch (jse.number)
			{
            		case 0: wjse->stick_x = jse.value;
                			break;
                case 1: wjse->stick_y = jse.value;
                        break;
                default:
                        break;
            }
		} else if (jse.type == JS_EVENT_BUTTON) 
		{
			if (jse.number < 10) 
			{
				switch (jse.value)
				{
                case 0:
                case 1: wjse->button[jse.number] = jse.value;
		                break;
	            default:
	    			        break;
	            }
	        }
		}
	}
    return 0;
}
\end{listing}
\subsection{Joystick.h}
\begin{listing}[language=C]
/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
#include "joy.c" 
/* Header for class Hello */

#ifndef _Included_Joystick
#define _Included_Joystick
#ifdef __cplusplus
extern "C" {
#endif
/*
 * Class:     Joystick
 * Method:    eventos
 * Signature: ()V
 */
JNIEXPORT void JNICALL Java_Joystick_eventos
  (JNIEnv *, jobject);

#ifdef __cplusplus
}
#endif
#endif
\end{listing}
\subsection{Joystick.c}
\begin{listing}[language=C]
#include <jni.h>
#include "Joystick.h"
#include <stdio.h>
JNIEXPORT void JNICALL Java_Joystick_eventos(JNIEnv *env, jobject obj){
	int fd, rc;
	int done = 0;

	jint valor1;
	jshort valor2;
	jint valor3;
	jint valor4;
	jint flag;

	struct js_event jse;
	
	if (fd>0)
	{
		fd = open_joystick();
			if (fd < 0)
			{
				printf("open failed.\n");
				//exit(1);
			}
	}
	jclass clase = (*env)->GetObjectClass(env,obj);
	jfieldID fieldIDtime = (*env)->GetFieldID(env,clase,"time","I");
	jfieldID fieldIDvalue = (*env)->GetFieldID(env,clase,"value","S");
	jfieldID fieldIDtype = (*env)->GetFieldID(env,clase,"type","I");
	jfieldID fieldIDnumber = (*env)->GetFieldID(env,clase,"number","I");
	jfieldID fieldIDflag = (*env)->GetFieldID(env,clase,"flag","I");
	if (fieldIDtime == NULL)
		return; // Excepcion lanzada

	if(fieldIDvalue == NULL)
		return;

	if(fieldIDtype == NULL)
		return;

	if(fieldIDnumber == NULL){
		printf("No esta el campo number\n");
		return;
	}

	valor1 = (*env)->GetIntField(env,obj,fieldIDtime);
	valor2 = (*env)->GetShortField(env,obj,fieldIDvalue);
	valor3 = (*env)->GetIntField(env,obj,fieldIDtype);
	valor4 = (*env)->GetIntField(env,obj,fieldIDnumber);
	flag = (*env)->GetIntField(env,obj,fieldIDflag);


	if(fd<0)
	{
		flag = 1;
		(*env)->SetIntField(env,obj,fieldIDflag,flag);
	}else
	{
		while (!done) {
			rc = read_joystick_event(&jse);
	        usleep(1000);
	        if (rc == 1)
	        {
	        	valor1 = jse.time;
	        	valor2 = jse.value;
	        	valor3 = (int)jse.type;
	       		valor4 = (int)jse.number;

	       		(*env)->SetIntField(env,obj,fieldIDtime,valor1);
	       		(*env)->SetShortField(env,obj,fieldIDvalue,valor2);
	       		(*env)->SetIntField(env,obj,fieldIDtype,valor3);
	       		(*env)->SetIntField(env,obj,fieldIDnumber,valor4);
	        }
		}
	}
return;
}
\end{listing}
\subsection{Joystick.java}
\begin{listing}
import java.util.ArrayList;
import java.util.EventObject;
import java.util.Iterator;
import java.util.List;

public class Joystick extends Thread{
	
	private int flag;
	private int time;
	private short value; 
	private int type;
	private int number;
	

	public Joystick(){
		
		this.time=0;
		this.value=0;
		this.type=0;
		this.number=0;
		this.flag=0;
	}

	public native void eventos();
	static {
		System.loadLibrary("joystick");
	}
	
	public int getTime(){
		return this.time;
	}

	public short getValue(){
		return this.value;
	}
	
	public int getType(){
		return this.type;
	}

	public int getNumber(){
		return this.number;
	}

	public int getFlag()
	{
		return this.flag;
	}
	public void run(){		
		this.eventos();		
	}
	
}
\end{listing}